package org.bingdianleil.control;

import java.util.List;

import javax.annotation.Resource;

import org.bingdianleil.service.StockShowService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.bingdianleil.util.Util;

@Controller
public class ChartsController {

	@Resource
	private StockShowService stockShowService;

	@RequestMapping("/line")
	public String hello(Model model,
			@RequestParam(value = "stockId", required = false, defaultValue = "sh600006") String stockId,
			@RequestParam(value = "dataDate", required = false, defaultValue = "") String dataDate) {
		model.addAttribute("stockId", stockId);
		model.addAttribute("dataDate", dataDate);

		if (Util.isNullorEmpty(dataDate)) {
			model.addAttribute("stockList", stockShowService.getStock(stockId, Util.getSysDate("yyyy-MM-dd")));
		} else {
			model.addAttribute("stockList", stockShowService.getStock(stockId, dataDate));
		}
		return "line";
	}

	@RequestMapping("/getLineData")
	@ResponseBody
	public List<List<String>> getLineData(Model model,
			@RequestParam(value = "stockId", required = false, defaultValue = "sh600006") String stockId,
			@RequestParam(value = "dataDate", required = false, defaultValue = "") String dataDate) {
		if (Util.isNullorEmpty(dataDate)) {
			return stockShowService.getStock(stockId, Util.getSysDate("yyyy-MM-dd"));
		}
		return stockShowService.getStock(stockId, dataDate);
	}

	@RequestMapping("/marketDayLine")
	public String marketDayLine(Model model,
			@RequestParam(value = "stockId", required = false, defaultValue = "sh600006") String stockId,
			@RequestParam(value = "bgDate", required = false, defaultValue = "") String bgDate,
			@RequestParam(value = "endDate", required = false, defaultValue = "") String endDate) {

		model.addAttribute("stockId", stockId);

		if (Util.isNullorEmpty(bgDate) || Util.isNullorEmpty(endDate)) {
			model.addAttribute("stockList",
					stockShowService.getStock(stockId, Util.getSysDate("yyyy-MM-dd"), Util.getSysDate("yyyy-MM-dd")));
			model.addAttribute("bgDate", Util.getSysDate("yyyy-MM-dd"));
			model.addAttribute("endDate", Util.getSysDate("yyyy-MM-dd"));
		} else {
			model.addAttribute("stockList", stockShowService.getStock(stockId, bgDate, endDate));
			model.addAttribute("bgDate", bgDate);
			model.addAttribute("endDate", endDate);
		}
		return "marketDayLine";
	}

}
